/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Brasil.java
 *
 * Created on 14/11/2010, 01:25:12
 */
package brasilquad;

import brasilquad.beans.ManipulaArquivo;
import brasilquad.beans.NoBruto;
import brasilquad.beans.NoQuad;
import brasilquad.utils.OrdenaCidades;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author rooke
 */
public class Brasil extends javax.swing.JFrame {

    // Frame.class
    Ponto mapa = new Ponto();
    ArrayList<NoBruto> brutList;
    NoQuad raiz;

    public Brasil() {
        initComponents();

        jFrame1.setVisible(true);
        jFrame1.setSize(170, 330);
        jFrame2.setVisible(false);
        jFrame2.setSize(260, 300);

        setTitle("Brasil");
        setSize(800, 640);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);


        // Pega os pontos do arquivo
        brutList = new ManipulaArquivo().lerArquivoMapa();

//        for (NoBruto noBruto : brutList) {
//            System.out.println(noBruto.getCidade());
//        }

        // Transforma o primeiro ponto do arquivo em n? e raiz
        NoBruto noBruto = brutList.get(0);

        raiz = new NoQuad();
        raiz.setGeonameId(noBruto.getGeonameId());
        raiz.setLatitude(noBruto.getLatitude());
        raiz.setLongitude(noBruto.getLongitude());
        raiz.setPosicaoArq(noBruto.getPosicaoArq());
        raiz.setFeatureClass(noBruto.getFeatureClass());
        raiz.setFeatureCode(noBruto.getFeatureCode());

        // constroi a arvore
        for (NoBruto bruto : brutList) {
            if (bruto != brutList.get(0)) {

                NoQuad no = new NoQuad();
                no.setGeonameId(bruto.getGeonameId());
                no.setLatitude(bruto.getLatitude());
                no.setLongitude(bruto.getLongitude());
                no.setPosicaoArq(bruto.getPosicaoArq());
                no.setFeatureClass(bruto.getFeatureClass());
                no.setFeatureCode(bruto.getFeatureCode());

                raiz.inserirNo(no);
            }
        }



    }

    @Override
    public void paint(Graphics g) {

        //Criamos um contexto gr?fico com a ?rea de pintura restrita
        //ao interior da janela.
        Graphics2D clip = (Graphics2D) g.create(getInsets().left, getInsets().top, getWidth() - getInsets().right, getHeight() - getInsets().bottom);
        //Pintamos o fundo do frame de branco
        clip.setColor(Color.WHITE);
        clip.fill(clip.getClipBounds());

        //plota o mapa
        plotar(raiz, null, clip);
        mapa.paint(g, 0, 0, Color.RED);


        clip.dispose();
    }

    private void plotar(NoQuad raiz, String featureCode, Graphics2D grap) {
        if (raiz != null) {

            int latitude = (int) (-raiz.getLatitude() * 15);
            int longitude = (int) (raiz.getLongitude() * 15);

            // Se todos est? selecionado
            if (todos.isSelected()) {
                mapa.plotaPreto(grap, longitude, latitude);
            } else {

                //se Demais pontos est? selecionado
                if (demais.isSelected()) {
                    if (!raiz.getFeatureCode().equals("BCH")
                            && !raiz.getFeatureCode().equals("BCHS")
                            && !raiz.getFeatureCode().contains("SCH")
                            && !raiz.getFeatureCode().equals("AIRP")
                            && !raiz.getFeatureCode().contains("PPL")) {
                        mapa.plotaPreto(grap, longitude, latitude);
                    }
                }
                //se Cidades est? selecionado
                if (cidades.isSelected()) {
                    // Cidades
                    if (raiz.getFeatureCode().contains("PPL")) {
                        mapa.plotaCinza(grap, longitude, latitude);
                    }
                }
                //se Aeroportos est? selecionado
                if (aeroportos.isSelected()) {
                    // Aeroportos
                    if (raiz.getFeatureCode().equals("AIRP")) {
                        mapa.plotaVerde(grap, longitude, latitude);
                    }
                }
                //se escolas est? selecionado
                if (escolas.isSelected()) {
                    //escolas
                    if (raiz.getFeatureCode().contains("SCH")) {
                        mapa.plotaAmarelo(grap, longitude, latitude);
                    }
                }
                //se praias est? selecionado
                if (praias.isSelected()) {
                    //praias
                    if (raiz.getFeatureCode().equals("BCH") || raiz.getFeatureCode().equals("BCHS")) {
                        mapa.plotaAzul(grap, longitude, latitude);
                    }
                }
            }



            plotar(raiz.getBaixoDireita(), featureCode, grap);
            plotar(raiz.getBaixoEsquerda(), featureCode, grap);
            plotar(raiz.getCimaDireita(), featureCode, grap);
            plotar(raiz.getCimaEsquerda(), featureCode, grap);

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        todos = new javax.swing.JCheckBox();
        cidades = new javax.swing.JCheckBox();
        praias = new javax.swing.JCheckBox();
        escolas = new javax.swing.JCheckBox();
        aeroportos = new javax.swing.JCheckBox();
        demais = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jFrame2 = new javax.swing.JFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jFrame1.setTitle("Opcoes");

        todos.setSelected(true);
        todos.setText("Todos os Pontos");
        todos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ativaOuDesativa(evt);
            }
        });

        cidades.setForeground(new java.awt.Color(132, 132, 132));
        cidades.setText("Cidades");
        cidades.setEnabled(false);
        cidades.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizaOMapa(evt);
            }
        });

        praias.setForeground(new java.awt.Color(0, 4, 255));
        praias.setText("Praias");
        praias.setEnabled(false);
        praias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizaOMapa(evt);
            }
        });

        escolas.setForeground(new java.awt.Color(226, 219, 1));
        escolas.setText("Escolas");
        escolas.setEnabled(false);
        escolas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizaOMapa(evt);
            }
        });

        aeroportos.setForeground(new java.awt.Color(1, 205, 7));
        aeroportos.setText("Aeroportos");
        aeroportos.setEnabled(false);
        aeroportos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizaOMapa(evt);
            }
        });

        demais.setText("Demais Pontos");
        demais.setEnabled(false);
        demais.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizaOMapa(evt);
            }
        });

        jLabel1.setText("Latitude: ");

        jLabel2.setText("Longitude: ");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cidades)
                    .addComponent(todos)
                    .addComponent(praias)
                    .addComponent(escolas)
                    .addComponent(aeroportos)
                    .addComponent(demais)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(todos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cidades)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(praias)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(escolas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aeroportos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(demais)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(127, Short.MAX_VALUE))
        );

        jFrame2.setTitle("Busca de Cidades");

        jScrollPane1.setViewportView(jTextPane1);

        jLabel3.setText("Latitude: ");

        jLabel4.setText("Longitude:");

        jLabel5.setText("Raio:");

        jTextField3.setText("0.03");

        jButton1.setText("Calcular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
        jFrame2.getContentPane().setLayout(jFrame2Layout);
        jFrame2Layout.setHorizontalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                    .addGroup(jFrame2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(4, 4, 4)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                    .addGroup(jFrame2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                    .addGroup(jFrame2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jFrame2Layout.setVerticalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(9, 9, 9)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                coordenadas(evt);
                abreBalaoEPegaCidadesNoRaio(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                coordenadas(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void recolore() {
        this.setVisible(false);
        this.setVisible(true);
    }

    private void ativaOuDesativa(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ativaOuDesativa
        // TODO add your handling code here:

        if (todos.isSelected()) {
            cidades.setEnabled(false);
            aeroportos.setEnabled(false);
            praias.setEnabled(false);
            escolas.setEnabled(false);
            demais.setEnabled(false);
            recolore();
        } else {
            cidades.setEnabled(true);
            aeroportos.setEnabled(true);
            praias.setEnabled(true);
            escolas.setEnabled(true);
            demais.setEnabled(true);
            recolore();
        }
    }//GEN-LAST:event_ativaOuDesativa

    private void atualizaOMapa(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_atualizaOMapa
        // TODO add your handling code here:
        recolore();
    }//GEN-LAST:event_atualizaOMapa

    private void coordenadas(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_coordenadas
        // TODO add your handling code here:
        double longitude = ((double) (evt.getX() - 1200)) / 15;
        double latitude = -((double) (evt.getY() - 100)) / 15;

        DecimalFormat decimal = new DecimalFormat("0.00");

        jLabel1.setText("Latitude: " + decimal.format(latitude));
        jLabel2.setText("Longitude: " + decimal.format(longitude));

    }//GEN-LAST:event_coordenadas

    private void abreBalaoEPegaCidadesNoRaio(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_abreBalaoEPegaCidadesNoRaio
        double longitude = ((double) (evt.getX() - 1200)) / 15;
        double latitude = -((double) (evt.getY() - 100)) / 15;

        DecimalFormat decimal = new DecimalFormat("0.0000");

        jTextField1.setText(decimal.format(latitude).replace(",", "."));
        jTextField2.setText(decimal.format(longitude).replace(",", "."));

        jFrame2.setVisible(true);
        pegaCidadesNoRaio();

    }//GEN-LAST:event_abreBalaoEPegaCidadesNoRaio

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        pegaCidadesNoRaio();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void pegaCidadesNoRaio() {
        jTextPane1.setText(" ");

         ArrayList<NoQuad> listaDeNos = new ArrayList<NoQuad>();

      
        if(demais.isSelected()||cidades.isSelected()||todos.isSelected())
        NoQuad.getCidadesNoRaio(this.raiz, Double.parseDouble(jTextField1.getText()), Double.parseDouble(jTextField2.getText()), Double.parseDouble(jTextField3.getText()), listaDeNos);
        if(escolas.isSelected())
       
        NoQuad.getEscolasNoRaio(this.raiz, Double.parseDouble(jTextField1.getText()), Double.parseDouble(jTextField2.getText()), Double.parseDouble(jTextField3.getText()), listaDeNos);
        if(praias.isSelected())
            
        NoQuad.getPraiasNoRaio(this.raiz, Double.parseDouble(jTextField1.getText()), Double.parseDouble(jTextField2.getText()), Double.parseDouble(jTextField3.getText()), listaDeNos);
        if(aeroportos.isSelected())
            
        NoQuad.getAeroportosNoRaio(this.raiz, Double.parseDouble(jTextField1.getText()), Double.parseDouble(jTextField2.getText()), Double.parseDouble(jTextField3.getText()), listaDeNos);

        NoBruto bruto;

        ArrayList<String> listaCidades = new ArrayList<String>();

        for (NoQuad noQuad : listaDeNos) {
            bruto = brutList.get(noQuad.getPosicaoArq());
            listaCidades.add(bruto.toString());
        }

        listaCidades = OrdenaCidades.Ordena(listaCidades);

        for(String cidade : listaCidades)
        {
            jTextPane1.setText(jTextPane1.getText() + cidade + "\n");
        }
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox aeroportos;
    private javax.swing.JCheckBox cidades;
    private javax.swing.JCheckBox demais;
    private javax.swing.JCheckBox escolas;
    private javax.swing.JButton jButton1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JFrame jFrame2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JCheckBox praias;
    private javax.swing.JCheckBox todos;
    // End of variables declaration//GEN-END:variables
}
